package com.sg.service.biz.app.nb;

import cn.hutool.core.bean.BeanUtil;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.biz.app.req.*;
import com.sg.dto.biz.app.res.*;
import com.wicket.okrapp.common.baseinfo.BaseInfoDO;
import com.wicket.okrapp.common.baseinfo.BaseInfoHolder;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
public class NbOffState {
    /**
     * code:rowToColumn
     * name:M3多个检查项转换为数据集格式
     * desc:undefined
     * gen by moon at 8/29/2022, 12:44:54 PM
     **/
    @Trace(operationName = "M3多个检查项转换为数据集格式")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public TransitionCheckItemRespDto transitionCheckItem(TransitionCheckItemReqDto reqDto) {
        TransitionCheckItemRespDto retData = new TransitionCheckItemRespDto();
        if (reqDto.getCheckItemCode1() != null && !reqDto.getCheckItemCode1().isEmpty()) {
            retData.getCheckItemList().add(reqDto.getCheckItemCode1());
        }
        if (reqDto.getCheckItemCode2() != null && !reqDto.getCheckItemCode2().isEmpty()) {
            retData.getCheckItemList().add(reqDto.getCheckItemCode2());
        }
        if (reqDto.getCheckItemCode3() != null && !reqDto.getCheckItemCode3().isEmpty()) {
            retData.getCheckItemList().add(reqDto.getCheckItemCode3());
        }
        if (reqDto.getCheckItemCode4() != null && !reqDto.getCheckItemCode4().isEmpty()) {
            retData.getCheckItemList().add(reqDto.getCheckItemCode4());
        }
        if (reqDto.getCheckItemCode5() != null && !reqDto.getCheckItemCode5().isEmpty()) {
            retData.getCheckItemList().add(reqDto.getCheckItemCode5());
        }
        if (reqDto.getCheckItemCode6() != null && !reqDto.getCheckItemCode6().isEmpty()) {
            retData.getCheckItemList().add(reqDto.getCheckItemCode6());
        }
        if (reqDto.getCheckItemCode7() != null && !reqDto.getCheckItemCode7().isEmpty()) {
            retData.getCheckItemList().add(reqDto.getCheckItemCode7());
        }
        if (reqDto.getCheckItemCode8() != null && !reqDto.getCheckItemCode8().isEmpty()) {
            retData.getCheckItemList().add(reqDto.getCheckItemCode8());
        }
        if (reqDto.getCheckItemCode9() != null && !reqDto.getCheckItemCode9().isEmpty()) {
            retData.getCheckItemList().add(reqDto.getCheckItemCode9());
        }
        return retData;
    }

    /**
     * code:collectionRemoveExistingData
     * name:M3去除已存在的被评对象目标周期（特殊方法）
     * desc:undefined
     * gen by moon at 8/29/2022, 12:46:36 PM
     **/
    @Trace(operationName = "M3去除已存在的被评对象目标周期（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public QueryFilterEvaObjTargetCycleListRespDto queryFilterEvaObjTargetCycleList(QueryFilterEvaObjTargetCycleListReqDto reqDto) {
        QueryFilterEvaObjTargetCycleListRespDto retData = new QueryFilterEvaObjTargetCycleListRespDto();
        List<String> filteredList = new ArrayList<>();
        for (String one : reqDto.getEvaObjTargetCycleList()) {
            if (!reqDto.getExistentEvaObjTargetCycleList().contains(one)) {
                filteredList.add(one);
            }
        }
        retData.setEvaObjTargetCycleList(filteredList);
        return retData;
    }

    /**
     * code:ATUO_INHERIT_NUM_VALUE
     * name:M3输出上级仪表盘分子加减值
     * desc:undefined
     * gen by moon at 8/29/2022, 12:47:07 PM
     **/
    @Trace(operationName = "M3输出上级仪表盘分子加减值")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public QueryOutputAddOrSubtNumValueDetailRespDto queryOutputAddOrSubtNumValueDetail(QueryOutputAddOrSubtNumValueDetailReqDto reqDto) {
        return BeanUtil.toBean(reqDto, QueryOutputAddOrSubtNumValueDetailRespDto.class);
    }

    /**
     * code:receptionService
     * name:M3-输入字段
     * desc:undefined
     * gen by moon at 8/29/2022, 12:47:29 PM
     **/
    @Trace(operationName = "M3-输入字段")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainReceiveFieldRespDto obtainReceiveField(ObtainReceiveFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ObtainReceiveFieldRespDto.class);
    }

    /**
     * code:publicFieldCache
     * name:M3更新公共字段缓存
     * desc:undefined
     * gen by moon at 9/8/2022, 3:45:41 AM
     **/
    @Trace(operationName = "M3更新公共字段缓存")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public RefreshPublicFieldCacheRespDto refreshPublicFieldCache(RefreshPublicFieldCacheReqDto reqDto) {
        if (BaseInfoHolder.contextHolder.get() != null) {
            BaseInfoHolder.contextHolder.remove();
        }
        BaseInfoDO domain = new BaseInfoDO();
        domain.setSpecSpaceId(reqDto.getSpaceId());
        domain.setSpecInductionRecordId(reqDto.getCreateInductionId());
        domain.setSpecAppId(reqDto.getAppId());
        CommonFunctionHelper.setBaseInfoToLocal(domain);
        return new RefreshPublicFieldCacheRespDto();
    }

    /**
     * code:getDataCount
     * name:M3-数据集计算完成数量（特殊方法）
     * desc:undefined
     * gen by moon at 9/13/2022, 12:40:06 AM
     **/
    @Trace(operationName = "M3-数据集计算完成数量（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public QueryDataSetCountQuantityAccomplishDetailRespDto queryDataSetCountQuantityAccomplishDetail(QueryDataSetCountQuantityAccomplishDetailReqDto reqDto) {
        // TODO ruizhe skai dong ; 9/13/2022, 12:40:06 AM
        return new QueryDataSetCountQuantityAccomplishDetailRespDto();
    }

    /**
     * code:receptionService
     * name:M3获取接收外部字段(特殊方法）
     * desc:undefined
     * gen by moon at 9/22/2022, 12:27:36 AM
     **/
    @Trace(operationName = "M3获取接收外部字段(特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainReceiveOutFieldRespDto obtainReceiveOutField(ObtainReceiveOutFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ObtainReceiveOutFieldRespDto.class);
    }

    /**
     * code:publicFieldCache
     * name:M3执行业务应用公共字段推送内存（特殊方法）
     * desc:undefined
     * gen by moon at 9/26/2022, 9:58:54 PM
     **/
    @Trace(operationName = "M3执行业务应用公共字段推送内存（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementBizPublicFieldPushMemoryRespDto implementBizPublicFieldPushMemory(ImplementBizPublicFieldPushMemoryReqDto reqDto) {
        BaseInfoDO domain = new BaseInfoDO();
        if (BaseInfoHolder.contextHolder.get() != null) {
            BeanUtil.copyProperties(BaseInfoHolder.contextHolder.get().getBaseInfo(), domain);
            BaseInfoHolder.contextHolder.remove();
        }
        if (reqDto.getSpaceId() != null) {
            domain.setSpecSpaceId(reqDto.getSpaceId());
        }

        if (reqDto.getCreateInductionId() != null) {
            domain.setSpecInductionRecordId(reqDto.getCreateInductionId());
        }

        if (reqDto.getAppId() != null) {
            domain.setSpecAppId(reqDto.getAppId());
        }

        if (reqDto.getHighestOrgID() != null) {
            domain.setSpecHighestOrgID(reqDto.getHighestOrgID());
        }

        if (reqDto.getUserId() != null) {
            domain.setSpecUserId(reqDto.getUserId());
        }

        if (reqDto.getOriginalRoleMemberId() != null) {
            domain.setSpecOriginalRoleMemberId(reqDto.getOriginalRoleMemberId());
        }

        CommonFunctionHelper.setBaseInfoToLocal(domain);
        return new ImplementBizPublicFieldPushMemoryRespDto();
    }

    /**
     * code:receptionService
     * name:M3执行业务应用构造出入参字段（特殊方法）
     * desc:undefined
     * gen by moon at 10/8/2022, 1:24:38 AM
     **/
    @Trace(operationName = "M3执行业务应用构造出入参字段（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementBizConstructAccessParameterFieldRespDto implementBizConstructAccessParameterField(ImplementBizConstructAccessParameterFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementBizConstructAccessParameterFieldRespDto.class);
    }

    /**
     * code:1
     * name:D3执行分析中层步骤房间(公共)
     * desc:undefined
     * gen by moon at 10/8/2022, 1:24:39 AM
     **/
    @Trace(operationName = "D3执行分析中层步骤房间(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementAnalysisMidStepRoomComRespDto implementAnalysisMidStepRoomCom(ImplementAnalysisMidStepRoomComReqDto reqDto) {
        // TODO ruizhe skai dong ; 10/8/2022, 1:24:39 AM
        return new ImplementAnalysisMidStepRoomComRespDto();
    }

    /**
     * code:dataFilterSpecRecBy
     * name:M3转换高中基层步骤（特殊方法）
     * desc:undefined
     * gen by moon at 10/10/2022, 3:36:55 AM
     **/
    @Trace(operationName = "M3转换高中基层步骤（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public TransitionTopMidBasicStepRespDto transitionTopMidBasicStep(TransitionTopMidBasicStepReqDto reqDto) {
        TransitionTopMidBasicStepRespDto retData = new TransitionTopMidBasicStepRespDto();
        Map<String, TopMidBasicStepDto> indexInfo = new HashMap<>();
        for (TopMidBasicStepDto one : reqDto.getTopMidBasicStepList()) {
            indexInfo.put(one.getStepNameCode(), one);
        }
        List<TopMidBasicStepDto> filteredList = new ArrayList<>(reqDto.getTopMidBasicStepList());
        if (reqDto.getExecuteTargetArrange() != null && !reqDto.getExecuteTargetArrange().isEmpty()) {
            String code = CommonFunctionHelper.humpToUnderline(reqDto.getExecuteTargetArrange());
            if (!reqDto.getExecuteTargetArrange().equals("1") && indexInfo.get(code) != null) {
                filteredList.remove(indexInfo.get(code));
            }
        }

        if (reqDto.getLeadTargetArrange() != null && !reqDto.getLeadTargetArrange().isEmpty()) {
            String code = CommonFunctionHelper.humpToUnderline(reqDto.getLeadTargetArrange());
            if (!reqDto.getLeadTargetArrange().equals("1") && indexInfo.get(code) != null) {
                filteredList.remove(indexInfo.get(code));
            }
        }

        if (reqDto.getTargetTaskDiv() != null && !reqDto.getTargetTaskDiv().isEmpty()) {
            String code = CommonFunctionHelper.humpToUnderline(reqDto.getTargetTaskDiv());
            if (!reqDto.getTargetTaskDiv().equals("1") && indexInfo.get(code) != null) {
                filteredList.remove(indexInfo.get(code));
            }
        }
        return retData;
    }

    /**
     * code:contDataNumAndDenoStatus
     * name:M3执行分析目标仪表盘完成状态（特殊方法）
     * desc:undefined
     * gen by moon at 10/19/2022, 8:47:21 PM
     **/
    @Trace(operationName = "M3执行分析目标仪表盘完成状态（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementTargetDashboardCompletionStatusRespDto implementTargetDashboardCompletionStatus(ImplementTargetDashboardCompletionStatusReqDto reqDto) {
        ImplementTargetDashboardCompletionStatusRespDto retData = new ImplementTargetDashboardCompletionStatusRespDto();
        Long count = 0L;
        for (TargetDashboardCompletionStatusDto one : reqDto.getTargetDashboardCompletionStatusList()) {
            Assert.isNull(one.getActualValue(), "M3执行分析目标仪表盘完成状态（特殊方法） 入参列表中出现 实际数值字段 为空", false);
            Assert.isNull(one.getTargetValue(), "M3执行分析目标仪表盘完成状态（特殊方法） 入参列表中出现 目标数值字段 为空", false);
            if (one.getTargetValue().longValue() == one.getActualValue().longValue()) {
                if (one.getTargetValue() == 0L) {
                    one.setIsComplete("FALSE");
                } else {
                    one.setIsComplete("TRUE");
                }
                count++;
            } else {
                one.setIsComplete("FALSE");
            }
            retData.getTargetDashboardCompletionStatusList().add(one);
        }
        retData.setInputNum(count);
        retData.setOutputNum(Long.valueOf(retData.getTargetDashboardCompletionStatusList().size()));
        return retData;
    }

    /**
     * code:dataListFusionByCode
     * name:M3执行检查项ID融合主体内容（特殊方法）
     * desc:undefined
     * gen by moon at 10/20/2022, 9:46:29 AM
     **/
    @Trace(operationName = "M3执行检查项ID融合主体内容（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementCheckItemFusionMainContentRespDto implementCheckItemFusionMainContent(ImplementCheckItemFusionMainContentReqDto reqDto) {
        ImplementCheckItemFusionMainContentRespDto retData = new ImplementCheckItemFusionMainContentRespDto();
        for (BizAppCheckItemStatDto oneBiz : reqDto.getBizAppCheckItemStatList()) {
            BizAppCheckItemStatDto elm = new BizAppCheckItemStatDto();
            BeanUtil.copyProperties(oneBiz, elm);
            retData.getBizAppCheckItemStatList().add(elm);
            for (CheckItemFusionMainContentDto oneCheck : reqDto.getCheckItemFusionMainContentList()) {
                if (oneCheck.getCheckItemCode().equals(oneBiz.getCheckItemCode())) {
                    BeanUtil.copyProperties(oneCheck, elm);
                }
            }
        }
        return retData;
    }

    /**
     * code:findAllLevelCodeList
     * name:M-公共查询找祖先列表（特殊方法）
     * desc:undefined
     * gen by moon at 12/30/2022, 8:26:22 PM
     **/
    @Trace(operationName = "M-公共查询找祖先列表（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public QueryLookAncestorNodeListRespDto queryLookAncestorNodeList(QueryLookAncestorNodeListReqDto reqDto) {
        QueryLookAncestorNodeListRespDto retData = new QueryLookAncestorNodeListRespDto();
        String oriCode = null;
        if (reqDto.getEvaObjTargetCycleCode() != null && !reqDto.getEvaObjTargetCycleCode().isEmpty()) {
            oriCode = reqDto.getEvaObjTargetCycleCode();
        }

        if (reqDto.getAssistNodeTypeObjectCode() != null && !reqDto.getAssistNodeTypeObjectCode().isEmpty()) {
            oriCode = reqDto.getAssistNodeTypeObjectCode();
        }

        if (reqDto.getComCode() != null && !reqDto.getComCode().isEmpty()) {
            oriCode = reqDto.getComCode();
        }

        if (oriCode == null || oriCode.isEmpty()) {
            return retData;
        }
        int dealSize = 4;
        while (oriCode.length() >= dealSize) {
            String oneCode = oriCode.substring(0, dealSize);
            if (oneCode != null && !oneCode.isEmpty()) {
                EvaObjTargetCycleDto elm = new EvaObjTargetCycleDto();
                elm.setEvaObjTargetCycleCode(oneCode);
                retData.getEvaObjTargetCycleList().add(elm);
                retData.getLookAncestorNodeList().add(oneCode);
            }
            dealSize = dealSize + 4;
        }
        retData.getLookAncestorNodeList().remove(oriCode);
        return retData;
    }

    /**
     * code:receptionService
     * name:M3执行获取出入参业务字段（特殊方法）
     * desc:undefined
     * gen by moon at 1/29/2023, 5:32:17 PM
     **/
    @Trace(operationName = "M3执行获取出入参业务字段（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementAccessParameterBizFieldRespDto implementAccessParameterBizField(ImplementAccessParameterBizFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementAccessParameterBizFieldRespDto.class);
    }

    /**
     * code:receptionService
     * name:M3接收上文出参仪表盘检查项模型值
     * desc:undefined
     * gen by moon at 3/18/2023, 3:34:13 PM
     **/
    @Trace(operationName = "M3接收上文出参仪表盘检查项模型值")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementAcceptFieldRespDto implementAcceptField(ImplementAcceptFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementAcceptFieldRespDto.class);
    }

    /**
     * code:receptionService
     * name:M3接收上文出参字段值
     * desc:undefined
     * gen by moon at 9/28/2023, 9:30:28 PM
     **/
    @Trace(operationName = "M3接收上文出参字段值")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementGainInParameterFieldsRespDto implementGainInParameterFields(ImplementGainInParameterFieldsReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementGainInParameterFieldsRespDto.class);
    }
    //手工接入方法
}
